package com.atguigui.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 字符串中的第一个唯一字符
 *
 * Project: leetcode
 * Package: PACKAGE_NAME
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2020/12/23 4:39 下午
 */
public class P387FirstUniqueCharacterInAString {
    public static void main(String[] args) {
        Solution solution = new P387FirstUniqueCharacterInAString().new Solution();
        System.out.println(solution.firstUniqChar("leetcode"));
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int firstUniqChar(String s) {
            Map<Character, Integer> frequency = new HashMap<Character, Integer>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                frequency.put(c, frequency.getOrDefault(c, 0) + 1);
            }
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (frequency.get(c) == 1) return i;
            }
            return -1;
        }
    }

}
